<?php
session_start();
include_once 'bd_conn_class/conexionMysql.php';

function verificarExisteUsuario($idUsuario, $tipoLogin, $correo){
    $con = new conexionMysql();
    $con->ConectarBD();
    
    $sql = " SELECT count(*) as c "
          ." FROM user ";
    
    /* Depende del tipo del login validamos en BD */
    if ($tipoLogin=="facebook"){
        $sql .= " WHERE user_uf_id = '".$idUsuario."' ";
    }
    if ($tipoLogin=="mail"){
        $sql .= " WHERE user_mail = '".$correo."' ";
    }
    $sql .= " AND user_status = 1 ";
    $rs = $con->ObtenerDataEnRecurso($sql);
    
    while ($fila = mysql_fetch_array($rs)){
        $existe = $fila["c"];
    }
    
    $con->DesconectarBD();
    
    return $existe;
}

function registrarUsuarioDeFacebook($user){
    include_once 'bd_conn_class/conexionMysql.php';
    $DB = new conexionMysql();
    $DB->ConectarBD();
    $query = " INSERT INTO user ( "
            ."   user_id, user_uf_id, user_name, user_fname, "
            ."   user_lname, user_sys_name, user_mail, "
            ."   user_profile_pic, user_g_id, "
            ."   user_ut_id, user_birthday, user_status, "
            ."   user_register_date)"
            ." VALUES ( "
            ."   NULL, '".$user->get_user_id()."', '".$user->get_user_name()."', '".$user->get_user_fname()."', "
            ."   '".$user->get_user_lname()."', '".$user->get_user_sys_name()."', '".$user->get_user_mail()."', "
            ."   '".$user->get_user_profile_pic()."', '".$user->get_user_g_id()."', "
            ."   '".$user->get_user_ut_id()."', '".$user->get_user_birthday()."', '".$user->get_user_status()."', "
            ."   NOW() );" ;

    $DB->EjecutarQuery($query);
    
    $query = " INSERT INTO user_facebook ( "
            ."   uf_id, uf_uname, uf_link )"
            ." VALUES ( "
            ."   '".$user->get_user_id()."', '".$user->get_user_uname()."', '".$user->get_user_fb_link()."' );" ;

    $DB->EjecutarQuery($query); 

    $query = " SELECT user_id as id FROM user WHERE user_uf_id = '".$user->get_user_id()."' ";
    $rs = $DB->ObtenerDataEnRecurso($query);
    
        while ($fila = mysql_fetch_array($rs)){
            $id = $fila["id"];
        }
    
    $DB->DesconectarBD();        
    return $id;  
}

function actualizarProfilePicture($id, $user){
    include_once 'bd_conn_class/conexionMysql.php';
    $DB = new conexionMysql();
    $DB->ConectarBD();
    $query = " UPDATE user "
            ." SET user_profile_pic = '".$user->get_user_profile_pic()."' "
            ." WHERE user_id = $id ";
    
    $DB->EjecutarQuery($query);
    $DB->DesconectarBD();
}

function registrarUsuarioXCorreo($user){
    include_once 'bd_conn_class/conexionMysql.php';
    $DB = new conexionMysql();
    $DB->ConectarBD();
    $query = " INSERT INTO user ( "
            ."   user_id,  "
            ."   user_mail, "
            ."   user_password, "
            ."   user_ut_id, user_status, "
            ."   user_register_date)"
            ." VALUES ( "
            ."   NULL, "
            ."   '".$user->get_user_mail()."', "
            ."   '".md5($user->get_user_password())."', "
            ."   '".$user->get_user_ut_id()."', '".$user->get_user_status()."', "
            ."   NOW() );" ;

    $DB->EjecutarQuery($query);

    $query = " SELECT user_id as id FROM user WHERE user_mail = '".$user->get_user_mail()."' ";
    $rs = $DB->ObtenerDataEnRecurso($query);
    
        while ($fila = mysql_fetch_array($rs)){
            $id = $fila["id"];
        }
    
    $DB->DesconectarBD();        
    return $id;  
}

function cargarSesionUsuario($idUsuario, $tipoLogin, $correo, $password){
    include_once 'recursos/fechas.php';
    $date = new fechas();
    
    $con = new conexionMysql();
    $con->ConectarBD();
    
    $sql = " SELECT *  "
          ." FROM user ";
    if ($tipoLogin == "facebook") {
        $sql .=" WHERE user_uf_id = '".$idUsuario."' ";
    }
    if ($tipoLogin == "mail") {
        $sql .=" WHERE user_mail = '".$correo."' AND user_password = '".md5($password)."' ";
    }
    $sql .=" AND user_status = 1 ";
    
    $rs = $con->ObtenerDataEnRecurso($sql);
    
    while ($fila = mysql_fetch_array($rs)){
        
        $_SESSION["g2uId"] = $fila["user_id"];
        
        $_SESSION["g2uSystemName"] = $fila["user_sys_name"];
        
        $_SESSION["g2uName"] = $fila["user_name"];
        $_SESSION["g2uMail"] = $fila["user_mail"];
        $_SESSION["g2uBirthday"] = $date->convertirFechatoNormal($fila["user_birthday"]);

        $_SESSION["g2uFirstName"] = $fila["user_fname"];
        $_SESSION["g2uLastName"] = $fila["user_lname"];
 
        $_SESSION["g2uProfilePic"] = $fila["user_profile_pic"];
        
        $_SESSION["g2uRegDate"] = $fila["user_register_date"];
    }
    
    $con->DesconectarBD();
}

function cargarSesionUsuarioFacebook($idUsuario){
    include_once 'recursos/fechas.php';
    $date = new fechas();
    
    $con = new conexionMysql();
    $con->ConectarBD();
    
    $sql = " SELECT *  "
          ." FROM user, user_facebook ";
    $sql .=" WHERE user_id = '".$idUsuario."' ";
    $sql .="     AND user_uf_id = uf_id ";
    $sql .="     AND user_status = 1 ";
    $sql .="     AND uf_status = 1 ";
        
    $rs = $con->ObtenerDataEnRecurso($sql);
    
    while ($fila = mysql_fetch_array($rs)){
        $_SESSION["g2uFacebookUserId"] = $fila["uf_id"];
        $_SESSION["g2uFacebookUserName"] = $fila["uf_uname"];
        $_SESSION["g2uFacebookProfileLink"] = $fila["uf_link"];
    }
    
    $con->DesconectarBD();
}
?>
